package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.ExamDao;
import com.woniuxy.dao.ExamineeDao;
import com.woniuxy.entity.*;
import com.woniuxy.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wenlei
 * @ClassName ExamServiceImpl
 * @Description TODO
 * @date 2022/5/4 16:03
 **/
@Service
public class ExamServiceImpl implements ExamService {
    @Autowired(required = false)
    ExamDao examDao;
    @Autowired(required = false)
    ExamineeDao examineeDao;

    /**
     *@author wen
     *@Discription 新增考试
     *@date 2022/5/4 16:05
     **/
    @Transactional//开启事务
    @Override
    public ResponseEntity addExam(ExamPublishexam examPublishexam) {

        int count=examDao.insertExamPublishexam(examPublishexam);
        int id = examPublishexam.getId();
        List<ExamJoinInfo> list=new ArrayList<>();

        List<ExamineeInformation> ei = examPublishexam.getExamineeInformations();
        for (ExamineeInformation e:ei) {
            ExamJoinInfo ex = new ExamJoinInfo();
            int id1 = e.getId();
            ex.setEid(id);
            ex.setSid(id1);
            list.add(ex);
        }
        int count1=examDao.addExamJoinInfo(list);
        if(count > 0 && count1 > 0){
            return ResponseEntity.SUCCESSFUL;
        }else {
            return ResponseEntity.FAIL;
        }
    }

    /**
     *@author wen
     *@Discription 查询考试列表
     *@date 2022/5/4 17:11
     *
     * @param examPublishexam*/
    @Override
    public ResponseEntity find(ExamPublishexam examPublishexam,int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<ExamPublishexam> list = examDao.findByCondition(examPublishexam);
        if (list !=null) {
            PageInfo<ExamPublishexam> pageInfo=PageInfo.of(list);
            return new ResponseEntity(200,"查询成功",pageInfo);
        }
        return ResponseEntity.FAIL;
    }

    /**
     *@author wen
     *@Discription 删除考试
     *@date 2022/5/4 21:17
     **/
    @Override
    public ResponseEntity deleteExam(int id) {
        int count =examDao.updateIsdelete(id);
        if(count > 0){
            return ResponseEntity.SUCCESSFUL;
        }else {
            return ResponseEntity.FAIL;
        }
    }

    /**
     *@author wen
     *@Discription 查询所有的考试列表
     *@date 2022/5/5 19:42
     **/
    @Override
    public ResponseEntity findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<ExamPublishexam> list=examDao.findAll();
        if(list != null){
            PageInfo<ExamPublishexam> pageInfo=PageInfo.of(list);
            return new ResponseEntity<>(200,"执行成功！",pageInfo);
        }
        return ResponseEntity.FAIL;

    }

    /**
     *@author wen
     *@Discription 查询所有考生info
     *@date 2022/5/7 14:42
     **/
    @Override
    public ResponseEntity findAllExamineeInfo() {
        List<ExamineeInformation> list=examineeDao.findAll();
        if(list != null){
            return new ResponseEntity<>(200,"执行成功！",list);
        }
        return ResponseEntity.FAIL;
    }

    /**
     *@author wen
     *@Discription 根据id删除参加考试的考生
     *@date 2022/5/5 19:41
     **/
    @Override
    public ResponseEntity deleteExamJoinInfoById(ExamPublishexam examPublishexam) {
        List<ExamJoinInfo> examJoinInfo = examPublishexam.getExamJoinInfo();
        List ids=new ArrayList();
        for (ExamJoinInfo e:examJoinInfo) {
            ids.add(e.getId());
        }
        int count =examineeDao.updateExamIneeInfoIsdelete(ids);
        if(count > 0){
            return ResponseEntity.SUCCESSFUL;
        }else {
            return ResponseEntity.FAIL;
        }
    }

    /**
     *@author wen
     *@Discription 修改考试信息
     *@date 2022/5/5 20:01
     **/
    @Override
    public ResponseEntity updateExam(ExamPublishexam examPublishexam) {
        int count=examDao.updateExam(examPublishexam);
        if(count > 0){
            return ResponseEntity.SUCCESSFUL;
        }else {
            return ResponseEntity.FAIL;
        }
    }

    /**
     *@author wen
     *@Discription 查询试卷信息
     *@date 2022/5/6 16:33
     **/
    @Override
    public ResponseEntity findPaperAtt() {
        List<PaperAttribute> list=examineeDao.findPaperAtt();
        if(list != null){
            return new ResponseEntity<>(200,"执行成功！",list);
        }
        return ResponseEntity.FAIL;
    }

    /**
     *@author wen
     *@Discription 批量删除考试
     *@date 2022/5/7 14:50
     **/
    @Override
    public ResponseEntity deleteExamMore(ExamPublishexam examPublishexam) {
        List<Integer> ids = examPublishexam.getIds();
        int count =examDao.updateExamIsdelete(ids);
        if(count > 0){
            return ResponseEntity.SUCCESSFUL;
        }else {
            return ResponseEntity.FAIL;
        }
    }

    /**
     *@author wen
     *@Discription 根据id查询参加考试了的考生信息
     *@date 2022/5/7 15:30
     **/
    @Override
    public ResponseEntity findExamineeByEid(int id) {
        List<ExamineeInformation> list=examineeDao.findExamineeByEid(id);
        if(list != null){
            return new ResponseEntity<>(200,"执行成功！",list);
        }
        return ResponseEntity.FAIL;
    }


}
